package com.miyake.dsp;

import java.util.ArrayList;
import java.util.List;


public class DspComposite {
	public interface DspCompositeHandler {
		void onRun();
	};
	
	private List<DspCompositeHandler> handlers = new ArrayList<DspCompositeHandler>();
	protected List<DspComposite> list = new ArrayList<DspComposite>();
	private Processor processor;
	protected boolean bypass;
	private Object userObject;
	private ComplexArray latestData;
	
	public DspComposite(Processor processor) {
		this.processor = processor;
	}
	
	public void add(Processor c) {
		list.add(new DspComposite(c));
	}
	
	public void add(DspComposite c) {
		list.add(c);
	}
	
	public void remove(DspComposite c) {
		list.remove(c);
	}
	
	public void run() {
		if (bypass) {
			latestData = processor.getSource();
		}
		else {
			latestData = processor.getProcessed();
		}
		
		fireRun();
		for (DspComposite c : list) {
			c.getProcessor().setSource(latestData);
			c.run();
		}
	}

	public Processor getProcessor() {
		return processor;
	}
	
	public void setProcessor(Processor processor) {
		this.processor = processor;
	}

	public void addHandler(DspCompositeHandler handler) {
		handlers.add(handler);
	}
	
	protected void fireRun() {
		for (DspCompositeHandler h : handlers) {
			h.onRun();
		}
	}

	public List<DspComposite> getList() {
		return list;
	}

	public boolean isBypass() {
		return bypass;
	}

	public void setBypass(boolean bypass) {
		this.bypass = bypass;
	}

	public Object getUserObject() {
		return userObject;
	}

	public void setUserObject(Object userObject) {
		this.userObject = userObject;
	}

	public void clear() {
		list.clear();
	}

	public Object getSetting() {
		return processor.getSetting();
	}

	public ComplexArray getLatestData() {
		return this.latestData;
	}

}
